package online.sixfish.common.security;



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private CustomAccessDeniedHandler customAccessDeniedHandler;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
              
                .antMatchers("/ums/user/login").anonymous()
                .antMatchers("/ums/user/register").anonymous()
                .antMatchers("/billboard/show").anonymous()
                .antMatchers("/post/list").anonymous()
                .antMatchers("/tip/today").anonymous()
                .antMatchers("/promotion/all").anonymous()
                .antMatchers("/admin/login").anonymous()
                .antMatchers("/notice/recent").anonymous()
                .antMatchers("/notice/detail/*").anonymous()
                .antMatchers("/api/image/*").anonymous()
                .antMatchers("/judge/one").anonymous()
                .antMatchers("/getQuestionList/*").anonymous()
                .antMatchers("/que/*").anonymous()
                .antMatchers("/article/list").anonymous()
                .antMatchers("/study-guide/list").anonymous()
                .antMatchers("/courseware/list").anonymous()
                .antMatchers("/tag/*").anonymous()




                // 对于加了权限注解的接口，不匿名访问
                .anyRequest().authenticated();



        //把token校验过滤器添加到过滤器链中
        http.addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

        //允许跨域
        http.cors();
        http.exceptionHandling().accessDeniedHandler(new ApiAccessDeniedHandler());
        http.exceptionHandling(except->except.authenticationEntryPoint(customAccessDeniedHandler));
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}


